package alipay.openplatform.edu.campus.jiuye.api.controller.system;

import alipay.openplatform.edu.campus.jiuye.api.utils.FrontApiLoginRequest;
import alipay.openplatform.edu.campus.jiuye.common.enums.ResultCode;
import alipay.openplatform.edu.campus.jiuye.common.utils.DateConverter;
import alipay.openplatform.edu.campus.jiuye.common.utils.ObjectUtils;
import alipay.openplatform.edu.campus.jiuye.service.dao.ApiResult;
import alipay.openplatform.edu.campus.jiuye.service.iservice.system.IAreaInfoService;
import alipay.openplatform.edu.campus.jiuye.service.services.system.AreaInfoService;
import org.apache.commons.beanutils.ConvertUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@FrontApiLoginRequest
@RequestMapping(value = "/jiuye/api")
public class AreaInfoController {


    static
    {
        ConvertUtils.register(new DateConverter(), java.util.Date.class);
    }


    //地区SERVICE对象
    @Autowired
    IAreaInfoService iAreaInfoService;

    //日志操作类
    private static Logger logger = LoggerFactory.getLogger(AreaInfoService.class);

    /**
     * 根据城市名称获取城市列表
     * @param cityName  城市名称
     * @return
     */
    @RequestMapping(value = "/getCityPage",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getCityPage(HttpServletRequest request,
                              @RequestParam(value = "cityName", required = true)String cityName,
                              @RequestParam(value = "pageNo", required = false,defaultValue = "1") Integer pageNo,
                              @RequestParam(value = "pageSize", required = false,defaultValue = "10") Integer pageSize

    ) {


        // 1. 验证参数
        if(ObjectUtils.hasNull(cityName)) {

            return ApiResult.build("-1","参数传递不正确!");
        }

        //2. 返回结果
        try {
            return iAreaInfoService.getCityPage(cityName,pageNo,pageSize);
        } catch (Exception e) {
            logger.info("根据城市名称获取城市列表|AreaInfoController|getCityPage|params:cityName={},pageNo={},pageSize={},e={}", cityName,pageNo,pageSize,e);
            return new ApiResult().build(ResultCode.SYSTEM_EXCEIPTION.getCode(), ResultCode.SYSTEM_EXCEIPTION.getName());

        }
    }


    /**
     * 根据城市编号删除城市信息
     * @param areaId  城市编号
     * @return
     */
    @RequestMapping(value = "/delCity")
    @ResponseBody
    public ApiResult delCity(@RequestParam(value = "areaId", required = true)String areaId)
    {


        // 1. 验证参数
        if(ObjectUtils.hasNull(areaId)) {

            return ApiResult.build("-1","参数传递不正确!");
        }

        //2. 返回结果
        try {
            return iAreaInfoService.delCity(areaId);

        } catch (Exception e) {
            logger.info("根据城市编号删除城市信息|AreaInfoController|delCity|params:areaId={},e={}", areaId,e);
            return new ApiResult().build(ResultCode.SYSTEM_EXCEIPTION.getCode(), ResultCode.SYSTEM_EXCEIPTION.getName());

        }
    }
}